package com.artisan.spider.util;

import com.artisan.spider.domain.SpDept;
import com.artisan.spider.domain.SpDictionary;
import com.artisan.spider.service.ISpDeptService;
import com.artisan.spider.service.ISpDictionaryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Page;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class PageUtil {


        public static void getSelectDic(String dickMark,int rootLength,Page page, String selector,List<SpDictionary> list,Boolean isClose) {

            // 获取opitions列表
            Locator locator = page.locator(selector);
            if (locator.count() > 0) {
                for (int i = 0; i < locator.count(); i++) {
                    String text = locator.nth(i).innerText();
                    String value = locator.nth(i).getAttribute("value");
                    log.info("下拉框的*****{}---》{}", text, value);
                    // 去除opition的全部
                    if(StringUtils.isNotBlank(value)){
                        SpDictionary spDictionary = new SpDictionary();
                        spDictionary.setId(UuidUtils.getUUid());
                        spDictionary.setDictName(text.replace("　",""));
                        spDictionary.setNameCode(value);
                        spDictionary.setDictMark(dickMark);
                        spDictionary.setStatus("1");
                        spDictionary.setCreateTime(DateUtil.getCurrentTime());
                        spDictionary.setSortNumber(i+1);

                        // 判断是不是曾经抓取过
                        QueryWrapper<SpDictionary> queryWrapper=new QueryWrapper();
                        queryWrapper.eq("dict_mark",dickMark);
                        int lawNumber=SpringContextUtil.getBean(ISpDictionaryService.class).count(queryWrapper);
                        if(lawNumber==0){
                            list.add(spDictionary);
                        }
                    }
                }
                if(isClose){
                    page.close();
                }

                for (SpDictionary spDictionary:list) {
                    String deptCode=spDictionary.getNameCode();
                    int bmlength=deptCode.length();
                    // 当前层级
                    Integer level = (bmlength - rootLength) / 2+1;
                    spDictionary.setTreeLevel(level);
                    if(bmlength==rootLength){
                        if("zy_xljb".equals(dickMark)){
                            spDictionary.setParentId("zyfg");
                        }else if("df_xljb".equals(dickMark)){
                            spDictionary.setParentId("dffggz");
                        }else{
                            spDictionary.setParentId("0");
                        }
                    }else{
                        if(bmlength==8){
                            spDictionary.setParentId(deptCode.substring(0,bmlength-3));
                        }else{
                            spDictionary.setParentId(deptCode.substring(0, bmlength - 2));
                        }
                    }
                }

                log.info("------------list转换完成----------");

            }
        }



    /**
     * @param rootLength  一级长度
     * @param page
     * @param selector
     * @param list
     * @param isClose
     */
    public static void getSelectDept(String dick_mark,int rootLength,Page page, String selector,List<SpDept> list,Boolean isClose) {
        
        // 获取opitions列表
        Locator locator = page.locator(selector);
        if (locator.count() > 0) {
            for (int i = 0; i < locator.count(); i++) {
                String text = locator.nth(i).innerText();
                String value = locator.nth(i).getAttribute("value");
                log.info("下拉框的*****{}---》{}", text, value);
                // 去除opition的全部
                if(StringUtils.isNotBlank(value)){
                    SpDept spDept = new SpDept();
                    spDept.setId(UuidUtils.getUUid());
                    spDept.setDeptName(text.replace("　",""));
                    spDept.setDeptCode(value);
                    spDept.setDeptGenre(dick_mark);
                    spDept.setFaguiType(dick_mark.split("_")[0]);
                    spDept.setStatus("1");
                    spDept.setCreateTime(DateUtil.getCurrentTime());
                    spDept.setSortNumber(i+1);

                    // 判断是不是曾经抓取过
                    QueryWrapper<SpDept> queryWrapper=new QueryWrapper();
                    queryWrapper.eq("dept_genre",dick_mark);
                    int lawNumber=SpringContextUtil.getBean(ISpDeptService.class).count(queryWrapper);
                    if(lawNumber==0){
                        list.add(spDept);
                    }
                }
            }
            if(isClose){
                page.close();
            }

            for (SpDept seDept:list) {
                String deptCode=seDept.getDeptCode();
                int bmlength=deptCode.length();
                // 当前层级
                int level = (bmlength - rootLength) / 2+1;
                seDept.setTreeLevel(String.valueOf(level));
                if(bmlength==rootLength||bmlength==0){
                    seDept.setDeptParentCode("0");
                    seDept.setDeptParentName("");
                }else{

                    if(bmlength==9){
                        seDept.setDeptParentCode(deptCode.substring(0,bmlength-4));
                    }else{
                        seDept.setDeptParentCode(deptCode.substring(0, bmlength - 2));
                    }

                    for (SpDept sd:list) {
                        if(sd.getDeptCode().equals(seDept.getDeptParentCode())){
                            seDept.setDeptParentName(sd.getDeptName().trim());
                            break;
                        }
                    }
                }

            }

            log.info("------------list转换完成----------");

        }
    }
}
